local stringtable=
{
"哎 \\Nd?",
"\\N{\\fe238}{\\fn方正大黑简体}{\\fs14}{\\b1}{\\c&HFFFFFF&}{\\3c&H2F2F2F&}{\\4c&H000000&}\"Meat\"?",
"{\\an8}{\\fn方正黑体简体\\fs18\\b1\\bord1\\shad1\\3c&H2F2F2F&}那间酒吧\\N营业",
"{\\an3\\pos(360,230)\\bord0\\b0\\shad1\\blur20\\fs20\\fn方正粗黑宋简体\\fad(0,300)\\t(0,1000,\\blur0)\\t(4800,5000,\\fsp10)\\1vc(FFFFFF,FFFFFF,818181,818181)\\4a&H9F&}@赛总 团队译制",
"Partlow承认所有空屋谋杀案 终身监禁 不得假释\\N{\\fnMicrosoft YaHei\\fs14\\2c&H111211&}Partlow pleads to all of the bodies in the vacants and takes life, no parole.",
"我看見他了 西斯科…\\N{\\fn微软雅黑}{\\fs14}{\\b0}{\\c&HFFFFFF&}{\\2c&H00111211F}{\\4c&H000000&}I saw him, Cisco…",
"- 但是怎么... - 老天\\N{\\fnMicrosoft YaHei\\fs14\\2c&H111211&}- But how can? - Jesus Christ.",
"- 但\\N{\\fnMicrosoft YaHei\\fs14\\2c&H111211&}-B",
"乖乖的\\N{\\fnMicrosoft YaHei\\fs14\\2c&H111211&}(crowd booing)",
"至少我知道 如果我有老婆或者未婚妻的话\\NI know that if I had a wife or a fiancée,",
"漫天都是糖霜  到处都是\\N{\\fe238}{\\fn方正大黑简体}{\\fs14}{\\b1}{\\c&HFFFFFF&}{\\3c&H2F2F2F&}{\\4c&H000000&}And it was in the air, everywhere.",
"然后她跪着  然后...\\N{\\fe238}{\\fn方正大黑简体}{\\fs14}{\\b1}{\\c&HFFFFFF&}{\\3c&H2F2F2F&}{\\4c&H000000&}and she is kneeling down, and...",
"-你后来有见过她吗  -有\\N{\\fe238}{\\fn方正大黑简体}{\\fs14}{\\b1}{\\c&HFFFFFF&}{\\3c&H2F2F2F&}{\\4c&H000000&}- Did you ever see her again? - Yeah.",
"好\\N{\\fe238}{\\fn方正大黑简体}{\\fs14}{\\b1}{\\c&HFFFFFF&}{\\3c&H2F2F2F&}{\\4c&H000000&}Sure.",
"-嗨  -嗨\\N{\\fe238}{\\fn方正大黑简体}{\\fs14}{\\b1}{\\c&HFFFFFF&}{\\3c&H2F2F2F&}{\\4c&H000000&}- Hey. - Hey.",
"-这是你最不需要的  -德西\\N{\\fe238}{\\fn方正大黑简体}{\\fs14}{\\b1}{\\c&HFFFFFF&}{\\3c&H2F2F2F&}{\\4c&H000000&}- That's the last thing you need. - Desi.",
"\"肉\"吗\\N{\\fe238}{\\fn方正大黑简体}{\\fs14}{\\b1}{\\c&HFFFFFF&}{\\3c&H2F2F2F&}{\\4c&H000000&}\"Meat\"?",
"对  就是\"肉\" 单音节\\N{\\fe238}{\\fn方正大黑简体}{\\fs14}{\\b1}{\\c&HFFFFFF&}{\\3c&H2F2F2F&}{\\4c&H000000&}Yes, \"Meat.\" One syllable.",
"因为吃奥施康定{\\fs14}[止痛药]{\\fs}出事\\N掉了几颗牙\\N{\\fe238}{\\fn方正大黑简体}{\\fs14}{\\b1}{\\c&HFFFFFF&}{\\3c&H2F2F2F&}{\\4c&H000000&}Got her teeth kicked in over some oxycontin.",
"因为吃奥施康定{\\fs14}[止痛药]{\\fs}出事掉了几颗牙\\N{\\fe238}{\\fn方正大黑简体}{\\fs14}{\\b1}{\\c&HFFFFFF&}{\\3c&H2F2F2F&}{\\4c&H000000&}Got her teeth kicked in over some oxycontin.",
"救命\\N啊\\N{\\fn微软雅黑}{\\b0}{\\fs14}{\\c&H3CF1F3&}{\\3c&000000&}Help!",
"救命啊\\NHelp!",
"說什么\\N{\\fn微软雅黑}{\\fs14}{\\b0}{\\c&HFFFFFF&}{\\2c&H00111211F}{\\4c&H000000&}Say what?",
"我想和你談談火風暴的事\\N{\\fnCronos Pro Subhead{\\fs14}{\\b0}{\\c&HFFFFFF&}{\\2c&H00111211F}{\\4c&H000000&}I wanted to talk to you about Firestorm.",
"約翰\\N{\\fe238}{\\fn方正大黑簡體}{\\fs14}{\\b0}{\\c&HFFFFFF&}{\\3c&H000000&}{\\4c&H000000&}John...",
}

--[[ find the \N in source, \N split the source into two parts, the first part
is Chinese, the rest part the english.
return chinese text and english text.
--]]
states = {}
positions = {}
function parse_text( source )
	for k,v in pairs(states) do
  states[k] = nil
	end
	for k,v in pairs(positions) do
  positions[k] = nil
	end

	local length = string.len(source)

	--[[ state machine to parse the text.
	0: normal text(Chinese), 1: line break state, 2: mark state start(like {font},
	3: mark state end, 4: ascii text char state(English),
	--]]
	local state = 0
	local i=1
	while i<=length do
		local subword = string.sub(source,i,i)
		if subword == " " then
			--ignore white space, make it simpler.
		elseif subword == "\\" then
			if string.sub(source,i,i+1) == "\\N" then
				state = 1
				record_state(state,i)
				-- skip the 'N'
				i=i+1
			else
				if state==2 then
				--ignore it, it is in the mark.
				else
					state = 4
					record_state(state,i)
				end
			end
		elseif subword == "{" then
			state = 2
			record_state(state,i)
		elseif subword == "}" and state == 2 then
			state = 3
			record_state(state,i)
		elseif is_ascii_char(subword) then
			if state==2 then
				--ignore it, it is in the mark.
			else
				state = 4
				record_state(state,i)
			end
		else
			if state==2 then
				--ignore it, it is in the mark.
			else
				state=0
				record_state(state,i)
			end
		end
		i=i+1
	end

	length = table.getn(states)

	-- reset the state and begin new function
	state = nil
	local split_position = nil
	local start_with_chs = nil
	local line_break_count = 0
	for i=1,length do
		if state==nil then
			state=states[i]
			if state==0 then
				start_with_chs=true
			elseif state==4 then
				start_with_chs=false
			end
		elseif states[i]==0 then
			if state==1 then
				if start_with_chs==true then
					-- it's also chs, only just like the chs subs contains \N
					state=states[i]
					if line_break_count>0 then
						split_position=nil
					end
				else
					-- may be this is a eng-chs subtitle
					state=states[i]
				end
			else
				if start_with_chs==nil then
					state=states[i]
					start_with_chs=true
				elseif start_with_chs==false then
					if line_break_count==0 then
						start_with_chs=true
						state=states[i]
					end
				else
					state=states[i]
				end
			end
		elseif states[i]==4 then
			if state==1 then
				if start_with_chs == true then
					--may it's chs-eng
					state=states[i]
				else
					--it's continue with eng
					state=states[i]
					if line_break_count>0 then
						split_position=nil
					end
				end
			else
				if start_with_chs==nil then
					start_with_chs = false
					state=states[i]
				else
					state=states[i]
				end
			end

			if start_with_chs==nil then
				start_with_chs=false
			end
		elseif states[i]==1 then
			state = states[i]
			if start_with_chs~=nil then
				line_break_count = line_break_count + 1
				split_position=i
			end
		end
	end

	if start_with_chs==nil then
		return nil,nil
	end

	if line_break_count==0 or split_position==nil then
		if start_with_chs == true then
			return source,nil
		else
			return nil,source
		end
	end

	local chs=string.sub(source,1,positions[split_position]-1)
	local eng=string.sub(source,positions[split_position]+2)
	if start_with_chs == false then
		local temp = chs
		eng=chs
		chs=temp
	end

	return chs,eng
end

function is_ascii_char(char)
	local ascii = string.byte(char,1)
	if ascii >=32 and ascii <= 127 then
		return true
	end

	return false
end

function record_state(new_state, position)
	local len = table.getn(states)
	if states[len]~=new_state then
		table.insert(states,new_state)
		table.insert(positions,position)
	end
end

for i,v in ipairs(stringtable) do

	print("\r\n--------")
	print(v)
	local chs,eng = parse_text(v)
	print(string.format("[chs]= %s",chs~=nil and chs or ""))
	print(string.format("[eng]= %s",eng~=nil and eng or ""))
end

--[[
for i=0,200 do
print(string.format("%d:%s",i,string.char(i)))
end
--]]
--print(string.byte("é"));
